import QtQuick 2.15
import QtQuick.Layouts 1.15
import QtQuick.Controls 2.15
import QtQuick.Shapes 1.15

MainView {
    id: root
    width: 800
    height: 391
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 125
    property int recWidth: 210
    property int currentPage:0
    Component.onCompleted: {

    }
    Timer{
        id:otaTimmer
        interval: 1200
        running: false
        repeat: false
        onTriggered: otaLoader.visible = false
    }
    MyBuzyIndicator{
        anchors.centerIn: parent
        id: oatLoader
        visible:false
        z:101
    }
    Dialog {
        anchors.centerIn: parent
        id: saveOKDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        title:qsTr("保存成功！")
        modal: true
        closePolicy: "CloseOnEscape"
        //onAccepted: saveOKDialog.close()
        z:100
        //onRejected: console.log("Cancel clicked")
    }
    Rectangle{
        id: gsMainRec
        anchors.fill: parent
        color:"transparent"
        Rectangle
        {
            id:page0
            visible: true
            color:"transparent"
            width: 800
            height: 350
            GridLayout{
                            id: otaLayout
                            rows: 2
                            columns: 4
                            anchors.top: parent.top
                            anchors.topMargin: 30
                            anchors.horizontalCenter: parent.horizontalCenter
                            rowSpacing: 40
                            Button
                                {
                                    Layout.row:0
                                    Layout.column:0
                                    Layout.preferredWidth: btnWidth
                                    Layout.preferredHeight: btnHeight
                                    // x: 152
                                    // y: 13
                                    text: "APN"
                                    font.pixelSize: 18
                                    background: Rectangle{
                                        color: btnColor
                                            }
                                    onClicked: {
                                        enterValuePanel.itemName="APN"
                                        enterValuePanel.itemValue=apn.text
                                        enterValuePanel.textInput=apn;
                                        enterValuePanel.visible=true;
                                        enterValuePanel.lastRec=gsMainRec
                                        gsMainRec.visible=false
                                    }
                                }
                                Rectangle {
                                    id: recAPN
                                    color: "transparent"
                                    border.width: 0
                                    border.color: "white"
                                    Layout.row:0
                                    Layout.column:1
                                    Layout.columnSpan: 3
                                    Layout.preferredWidth: 555
                                    Layout.preferredHeight: btnHeight
                                    // 绘制一条边框
                                    Shape {
                                        anchors.fill: parent
                                        ShapePath {
                                            strokeWidth: 3
                                            strokeColor: "white"
                                            strokeStyle: ShapePath.SolidLine
                                            startX: 0
                                            startY: recAPN.height
                                            PathLine {
                                                x: recAPN.width
                                                y: recAPN.height
                                            }
                                        }
                                    }
                                    TextInput{
                                        id:apn
                                        anchors.fill: parent
                                        //echoMode: TextInput.Password
                                        text:mymodbus.apn
                                        color:"white"
                                        font.pixelSize: 25
                                        horizontalAlignment: Text.AlignHCenter
                                        verticalAlignment: Text.AlignBottom
                                        MouseArea {
                                               anchors.fill: parent
                                               onClicked: {
                                                   enterValuePanel.itemName="APN"
                                                   enterValuePanel.itemValue=apn.text
                                                   enterValuePanel.textInput=apn;
                                                   enterValuePanel.visible=true;
                                                   enterValuePanel.lastRec=gsMainRec
                                                   gsMainRec.visible=false
                                               }
                                           }

                                    }
                                }
                                Button
                                {
                                    Layout.row:1
                                    Layout.column:0
                                    Layout.preferredWidth: btnWidth
                                    Layout.preferredHeight: btnHeight
                                    // x: 152
                                    // y: 13
                                    text: "URL"
                                    font.pixelSize: 18
                                    background: Rectangle{
                                        color: btnColor
                                            }
                                    onClicked: {
                                        enterValuePanel.itemName="URL"
                                        enterValuePanel.itemValue=url.text
                                        enterValuePanel.textInput=url;
                                        enterValuePanel.visible=true;
                                        enterValuePanel.lastRec=gsMainRec
                                        gsMainRec.visible=false
                                    }
                                }

                                Rectangle {
                                    id: recURL
                                    color: "transparent"
                                    border.width: 0
                                    border.color: "white"
                                    Layout.row:1
                                    Layout.column:1
                                    Layout.columnSpan: 3
                                    Layout.preferredWidth: 555
                                    Layout.preferredHeight: btnHeight
                                    // 绘制一条边框
                                    Shape {
                                        anchors.fill: parent
                                        ShapePath {
                                            strokeWidth: 3
                                            strokeColor: "white"
                                            strokeStyle: ShapePath.SolidLine
                                            startX: 0
                                            startY: recURL.height
                                            PathLine {
                                                x: recURL.width
                                                y: recURL.height
                                            }
                                        }
                                    }
                                    Flickable{
                                        width: 555
                                        height: btnHeight
                                        contentWidth: url.width
                                        contentHeight: url.height
                                        clip: true
                                        TextInput{
                                            id:url
                                            width:url.text.length*30
                                            height:btnHeight
                                            //width:recWidth
                                            //echoMode: TextInput.Password
                                            text:mymodbus.url
                                            color:"white"
                                            font.pixelSize: 25
                                            horizontalAlignment: Text.AlignLeft
                                            verticalAlignment: Text.AlignBottom

                                        }

                                        MouseArea {
                                            width: 555
                                            height: btnHeight
                                               onClicked: {
                                                   enterValuePanel.itemName="URL"
                                                   enterValuePanel.itemValue=url.text
                                                   enterValuePanel.textInput=url;
                                                   enterValuePanel.visible=true;
                                                   enterValuePanel.lastRec=gsMainRec
                                                   gsMainRec.visible=false
                                               }
                                           }
                                    }
                                }
                        }
        }


        Button
        {
            id:btnMqttSettingsOK
            x: 572
            y: 281
            //x: 675
            //y: 20
            width: 100
            height: 100
            background: Rectangle{
                border.width: 0
                color: "#C070B0"
                    }

            text: qsTr("确定")
            font.pixelSize: 25
            palette.buttonText: "white"
            Connections {
                target: btnAdvSettingsOK
                function onClicked() {
                    if(oatLoader.visible){
                        return;
                    }
                    oatLoader.visible=true;
                    //slotSaveMqttParas(QString hostName, int port, QString topic, QString userName, QString password, int isOpen, int interval, QString truckNo, QString clientID)
                    var res =mymodbus.saveOTAPara(apn.text, url.text)
                    var res1 = mymodbus.slotOTADownload();
                    if(res1)
                    {
                        saveOKDialog.title=qsTr("下载完成，请重启完成升级！")
                        saveOKDialog.open()
                        saveOKDialog.accepted.connect(function() {
                                                saveOKDialog.close()
                                            })
                    }
                    else
                    {
                        saveOKDialog.title=qsTr("下载失败，请稍候重试！")
                        saveOKDialog.open()
                        saveOKDialog.accepted.connect(function() {
                                                saveOKDialog.close()
                                            })
                    }
                    oatLoader.visible=false;
                }
            }
        }
       Button
        {
            id:btnReturn
            x: 688
            y: 281
            //x: 675
            //y: 20
            width: 100
            height: 100
            background: Rectangle{
                border.width: 0
                color: "#B0B0D0"
                    }

            text: qsTr("返回")
            font.pixelSize: 25
            palette.buttonText: "white"
            //Connections {
            //    target: btnReturn
                onClicked: {
                    //rectangle.selectedView = 4
                    bottomBar.selected=2
//                    myLoader.visible=false;
//                    myLoader.visible=true
                    myLoader.source="Upgrade.qml"
                }
            //}
        }

    }

        EnterValuePanel{
          id:enterValuePanel
          width: 800
          height: 391
          visible:false
    }

}

